﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using bdnt_pets_system_Entity;
using System.Windows.Data;
using bdnt_pets_system.Tools;
using bdnt_pets_system.Resources;

namespace bdnt_pets_system.PageViews.Failures
{
    public partial class NewFailureInfoWindow : BaseWindow
    {
        DataServiceReference.DataServiceClient dataClient;
        public NewFailureInfoWindow(Tbl_FailureInfo cuTbl_FailureInfo)
        {
            InitializeComponent();
            if (cuTbl_FailureInfo.Id == 0) 
                this.Title =MyStrings.xjgz;
            else this.Title =MyStrings.xggz;

            dataClient = new DataServiceReference.DataServiceClient();
            CuTbl_FailureInfo = cuTbl_FailureInfo;
            BingData();
            InitEvnet();
        }
        public Tbl_FailureInfo CuTbl_FailureInfo
        {
            get { return this.DataContext as Tbl_FailureInfo; }
            set { this.DataContext = value; }
        }

        void BingData()
        {
            tbFailureNo.SetBinding(TextBox.TextProperty, new Binding("FailureNo")
             {
                 Mode = BindingMode.TwoWay,
                 NotifyOnValidationError = true,
                 ValidatesOnExceptions = true
             });

            tbFailureName.SetBinding(TextBox.TextProperty, new Binding("FailureName")
            {
                Mode = BindingMode.TwoWay,
                NotifyOnValidationError = true,
                ValidatesOnExceptions = true
            });
            tben_FailureName.SetBinding(TextBox.TextProperty, new Binding("en_FailureName")
            {
                Mode = BindingMode.TwoWay,
                NotifyOnValidationError = true,
                ValidatesOnExceptions = true
            });
            tbRemark.SetBinding(TextBox.TextProperty, new Binding("Remark") { Mode = BindingMode.TwoWay });

            tbFC.SetBinding(TextBox.TextProperty, new Binding("FC")
            {
                Mode = BindingMode.TwoWay
            });

        }

        void InitEvnet()
        {
            btnOk.Click += new RoutedEventHandler(btnOk_Click);
            btnCancel.Click += new RoutedEventHandler(btnCancel_Click);
            dataClient.SaveFailureInfoCompleted += new EventHandler<DataServiceReference.SaveFailureInfoCompletedEventArgs>(dataClient_SaveFailureInfoCompleted);
            dataClient.GetPartListByConditionCompleted += new EventHandler<DataServiceReference.GetPartListByConditionCompletedEventArgs>(dataClient_GetPartListByConditionCompleted);
            InitData();
        }

        void InitData()
        {
            dataClient.GetPartListByConditionAsync(null, 1000, 1);
        }

        void dataClient_GetPartListByConditionCompleted(object sender, DataServiceReference.GetPartListByConditionCompletedEventArgs e)
        {
            var cuPts = e.Result;
            cbPartName.ItemsSource = cuPts;
            if (SessionManager.IsCN_ZH)
                cbPartName.DisplayMemberPath = "PartName";
            else cbPartName.DisplayMemberPath = "en_PartName";
            cbPartName.SelectedValuePath = "PartNo";
            if (!string.IsNullOrEmpty(CuTbl_FailureInfo.PartNo))
            {
                var cuo = cuPts.FirstOrDefault(p => p.PartNo == CuTbl_FailureInfo.PartNo);
                if (cuo == null)
                    cbPartName.SelectedIndex = -1;
                cbPartName.SelectedIndex = cuPts.IndexOf(cuo);
            }
        }

        void dataClient_SaveFailureInfoCompleted(object sender, DataServiceReference.SaveFailureInfoCompletedEventArgs e)
        {
            if (e.Result)
                this.DialogResult = true;
            else
                errorMsg.SetBinding(TextBlock.TextProperty, new Binding("Strings.bcsb") { Source = CuLc });
        }

        void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        /// <summary>
        /// 字段验证方法
        /// </summary>
        void BindgExpression()
        {           
            tbFailureNo.GetBindingExpression(TextBox.TextProperty).UpdateSource();
            tbFailureName.GetBindingExpression(TextBox.TextProperty).UpdateSource();
            tben_FailureName.GetBindingExpression(TextBox.TextProperty).UpdateSource();
           
        }

        void btnOk_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                BindgExpression();
                if (vs.Errors.Count > 0)
                {
                    errorMsg.Text =MyStrings.sjtxbq;
                    return;
                }
                if (cbPartName.SelectedValue != null)
                    CuTbl_FailureInfo.PartNo = cbPartName.SelectedValue.ToString();
                dataClient.SaveFailureInfoAsync(CuTbl_FailureInfo);
            }
            catch (Exception)
            {

            }
        }
    }
}
